package com.ocom.webapi.service.impl;

import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ocom.common.dto.MealMenuBDTO;
import com.ocom.common.entity.card.EntityXfMealInfo;
import com.ocom.common.entity.payconfig.EntityDishesPrice;
import com.ocom.common.entity.payconfig.EntityMealPlan;
import com.ocom.common.enums.ResultEnum;
import com.ocom.common.request.payconfig.GetTollPointInfoRequest;
import com.ocom.common.request.payconfig.MealPlanListRequest;
import com.ocom.common.resp.Result;
import com.ocom.common.utils.ResultUtil;
import com.ocom.common.utils.StringUtil;
import com.ocom.common.vo.dishes.DishesInfoListVo;
import com.ocom.common.vo.webapi.MealPlanVo;
import com.ocom.webapi.feign.PayConfigClientService;
import com.ocom.webapi.service.ImportMealMenuBService;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.time.Instant;
import java.time.LocalDate;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.*;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;

@Service
@Slf4j
public class ImportMealMenuBServiceImpl implements ImportMealMenuBService {

	@Autowired
	private PayConfigClientService payConfigClientService;


	/**
	 * 检查排餐名是否已经存在
	 *
	 * @param planname
	 * @return
	 */
	@Override
	public boolean checkPlanNameExist(Integer comId, String planname) {
		boolean reulst = false;
		MealPlanListRequest request = new MealPlanListRequest();
		request.setComId(Long.valueOf(comId));
		request.setMerchId(0);
		Result t = payConfigClientService.mealPlanList(request);

		List list = (List) t.getData();


		List<EntityMealPlan> reult = new ArrayList<>();
		list.forEach(item -> {
			EntityMealPlan itemVo = JSON.parseObject(JSON.toJSONString(item), EntityMealPlan.class);
			reult.add(itemVo);
		});

		List<EntityMealPlan> l = reult.stream().filter(item -> item.getPlanName().equals(planname)).collect(Collectors.toList());

		if (!ObjectUtils.isEmpty(l)) {
			reulst = true;
		}

		return reulst;
	}


	@Override
	public Result<String> importMealMenuB(Integer comId, MultipartFile file, String planName,
																				Integer pointId, Integer dishesClassId,
																				LocalDate startDate, LocalDate stopDate, String remark,String tagId) throws Exception {


		InputStream is;
		try {
			is = file.getInputStream();
		} catch (IOException e) {
			throw new RuntimeException(e);
		}

		XSSFWorkbook sheets = new XSSFWorkbook(is);
		is.close();

    Integer mchId = getMchIdByPointId(pointId, comId);
		Map<String, String> abc = new HashMap<>();
		Map<String, Map<String, Integer>> seriaNumber = new HashMap<>();
		List<EntityXfMealInfo> list = GetMealNameInfo(comId);

		for (EntityXfMealInfo item : list) {
			XSSFSheet sheet = sheets.getSheet(item.getMealName());
			if (ObjectUtils.isEmpty(sheet)) {
				continue;
			}

			Map<String, Integer> mealNoSeriaNumber = findserialNumber(sheet);
      if (CollectionUtils.isEmpty(mealNoSeriaNumber)){
        continue;
      }
			Map<String, String> number = convertMap(mealNoSeriaNumber, sheet.getLastRowNum());


			for (int i = 1; i < number.size() + 1; i++) {
				String val = number.get(String.valueOf(i));
				Map<String, String> dayMap = parseData(val, sheet, startDate);


				/**
				 * 一共读5列
				 */
				for (Integer k = 1; k < 6; k++) {
					LocalDate localDate = startDate.plusDays(k - 1);
					String key = localDate.format(DateTimeFormatter.ofPattern("dd"));
					String values = dayMap.get(key);
					if (StringUtil.isEmpty(values)) {
						continue;
					}

					String key1 = numberToLetter(i) + key + "_" + item.getMealNo();
					abc.put(key1, values);

				}

			}

			seriaNumber.put(item.getMealNo(), mealNoSeriaNumber);
		}

		if (ObjectUtils.isEmpty(abc)) {
			return ResultUtil.error(ResultEnum.Err_4401);
		}

		Map<String, DishesInfoListVo> updateDishes = updateDishes(comId, abc, dishesClassId, mchId);
		updateDishesPrice(comId, abc, pointId, updateDishes, mchId);
		int days = (int) ChronoUnit.DAYS.between(startDate, stopDate) + 1;
		Integer mealPlanId = newMealPlan(comId, pointId, planName,
        Date.from(startDate.atStartOfDay(ZoneId.systemDefault()).toInstant()),
        Date.from(stopDate.atStartOfDay(ZoneId.systemDefault()).toInstant()),
				days, remark, mchId,tagId);

		MealMenuBDTO dto = new MealMenuBDTO();
		dto.setMealPlanId(mealPlanId);
    dto.setMerchId(mchId);
		dto.setPointId(pointId);
		dto.setComId(Long.valueOf(comId));
		List<MealMenuBDTO.DishesListDTO> dishesList = new ArrayList<>();
		dto.setDishesList(dishesList);
		for (EntityXfMealInfo item : list) {
			Map<String, Integer> mealSerMap = seriaNumber.get(item.getMealNo());
			if (CollectionUtils.isEmpty(mealSerMap)) {
				continue;
			}
			for (int i = 1; i < mealSerMap.size() + 1; i++) {
				for (int k = 1; k < 6; k++) {
					LocalDate localDate = startDate.plusDays(k - 1);
					String key = localDate.format(DateTimeFormatter.ofPattern("dd"));
					key = numberToLetter(i) + key + "_" + item.getMealNo();
					if (abc.keySet().contains(key)) {
						MealMenuBDTO.DishesListDTO dishesListDTO = new MealMenuBDTO.DishesListDTO();
						dishesListDTO.setDays(k);
						dishesListDTO.setDishesId(getDishesIdByName("套餐" + key, comId));
						dishesListDTO.setKey(key);
						dishesListDTO.setMealNo(Integer.valueOf(item.getMealNo()));
						dishesListDTO.setValue(abc.get(key));
						dishesList.add(dishesListDTO);
					}

				}
			}

		}

    log.info("新增排餐菜单:{}", JSON.toJSONString(dto));
		payConfigClientService.saveMealMenuB(dto);
		return ResultUtil.success();
	}


	@Override
	public Result<String> importMealMenuC(Integer comId, MultipartFile file, String planName,
										  Integer pointId, Integer dishesClassId,
										  LocalDate startDate, LocalDate stopDate, String remark,String tagId) throws Exception {

		Integer mchId = getMchIdByPointId(pointId, comId);
		Map<String, String> abc = new HashMap<>();
		List<EntityXfMealInfo> list = GetMealNameInfo(comId);
		Map<String,Map<String, MealPlanVo>> dataMap = new HashMap<>();
		for (EntityXfMealInfo item : list) {
			ExcelReader reader;
			try {
				reader = ExcelUtil.getReader(file.getInputStream(),item.getMealName());
			}catch (Exception e){
				continue;
			}

			MealPlanVo.MealExcelVo mealExcelVo = parseMealMenuCData(reader, startDate,stopDate,item.getMealNo());  //改这里
			abc.putAll( mealExcelVo.getMealData());
			dataMap.put(item.getMealNo(),mealExcelVo.getDataMap());
		}

		if (ObjectUtils.isEmpty(abc)) {
			return ResultUtil.error(ResultEnum.Err_4401);
		}

		Map<String, DishesInfoListVo> updateDishes = updateDishesC(comId, abc, dishesClassId, mchId);
		updateDishesPrice(comId, abc, pointId, updateDishes, mchId);
		int days = (int) ChronoUnit.DAYS.between(startDate, stopDate) + 1;
		Integer mealPlanId = 0;
		try {
			mealPlanId= newMealPlan(comId, pointId, planName,
					Date.from(startDate.atStartOfDay(ZoneId.systemDefault()).toInstant()),
					Date.from(stopDate.atStartOfDay(ZoneId.systemDefault()).toInstant()),
					days, remark, mchId,tagId);
		}catch (Exception e){
			return ResultUtil.error(ResultEnum.Err_4402);
		}

		MealMenuBDTO dto = new MealMenuBDTO();
		dto.setMealPlanId(mealPlanId);
		dto.setMerchId(mchId);
		dto.setPointId(pointId);
		dto.setComId(Long.valueOf(comId));
		List<MealMenuBDTO.DishesListDTO> dishesList = new ArrayList<>();
		dto.setDishesList(dishesList);
		for (EntityXfMealInfo item : list) {
			Map<String, MealPlanVo> mealSerMap = dataMap.get(item.getMealNo());
			if (CollectionUtils.isEmpty(mealSerMap)) {
				continue;
			}
			for(String mealKey: mealSerMap.keySet()){
				MealPlanVo mealPlanVo = mealSerMap.get(mealKey);
				MealMenuBDTO.DishesListDTO dishesListDTO = new MealMenuBDTO.DishesListDTO();
				Instant instant = mealPlanVo.getDate().toInstant();
				LocalDate localDate = instant.atZone(ZoneId.systemDefault()).toLocalDate();
				mealPlanVo.getMealNameList().remove(0);
				dishesListDTO.setDays(localDate.getDayOfWeek().getValue());
				dishesListDTO.setDishesId(getDishesIdByCode( mealPlanVo.getMealPlanKey(), comId));
				dishesListDTO.setKey(mealKey);
				dishesListDTO.setMealNo(Integer.valueOf(item.getMealNo()));
				dishesListDTO.setValue(String.join("+",mealPlanVo.getMealNameList()));
						dishesList.add(dishesListDTO);
			}
		}
		log.info("新增排餐菜单:{}", dto);
		payConfigClientService.saveMealMenuB(dto);
		return ResultUtil.success();
	}

	private Integer getDishesIdByName(String name, Integer comId) {
		List<DishesInfoListVo> list = getAllDishes(Long.valueOf(comId));
		List<DishesInfoListVo> l = list.stream().filter(item -> item.getDishesName().equals(name)).collect(Collectors.toList());
		if (ObjectUtils.isEmpty(l)) {
			return 0;
		} else {
			Integer i = l.get(0).getDishesId();
			return i;
		}

	}
	private Integer getDishesIdByCode(String code, Integer comId) {
		List<DishesInfoListVo> list = getAllDishes(Long.valueOf(comId));
		List<DishesInfoListVo> l = list.stream().filter(item -> item.getDishesCode().equals(code)).collect(Collectors.toList());
		if (ObjectUtils.isEmpty(l)) {
			return 0;
		} else {
			Integer i = l.get(0).getDishesId();
			return i;
		}

	}


  private Integer getMchIdByPointId(Integer pointId, Integer ocomId){
    Integer mchId = 0;
    GetTollPointInfoRequest request = new GetTollPointInfoRequest();
    request.setPoint_id(pointId);
    request.setCom_id(Long.valueOf(ocomId));
    Result r = payConfigClientService.getTollPointInfo(request);
    LinkedHashMap data = (LinkedHashMap)r.getData();
    List l =  (List)data.get("list");
    if(!ObjectUtils.isEmpty(l)) {
      LinkedHashMap o = (LinkedHashMap)l.get(0);
      mchId = (Integer) o.get("merchId");

    }
    System.out.println(r);
    return mchId;
  }


	/**
	 * 更新套餐,
	 * 返回key, DishesInfoListVo
	 */
	private Map<String, DishesInfoListVo> updateDishes(Integer comId, Map<String, String> dishes, Integer dishedClassId, Integer mchId) {
		Map<String, DishesInfoListVo> result = new HashMap<>();
		List<DishesInfoListVo> allDishes = getAllDishes(Long.valueOf(comId));
		for (Map.Entry<String, String> item : dishes.entrySet()) {
			List<DishesInfoListVo> itemDishes = allDishes.stream().filter(dishesInfoListVo -> dishesInfoListVo.getDishesName().equals("套餐" + item.getKey())).collect(Collectors.toList());
			if (ObjectUtils.isEmpty(itemDishes)) {
				//插入菜品
				DishesInfoListVo vo = new DishesInfoListVo();
				vo.setRemark(item.getValue());
				vo.setDishesName("套餐" + item.getKey());
				vo.setDishesCode(item.getKey());
				vo.setComId(Long.valueOf(comId));
				vo.setDishesClassId(dishedClassId);
				vo.setStatus((byte) 1);
				vo.setSort(0);
				vo.setMerchId(mchId);
				Result r = payConfigClientService.newDishes((JSONObject) JSONObject.toJSON(vo));
				log.info("插入菜品结果:【{}】参数,【{}】" + r,JSON.toJSONString(vo));
       			 LinkedHashMap data = (LinkedHashMap)r.getData();
        		vo = JSON.parseObject(JSON.toJSONString(data), DishesInfoListVo.class);
				result.put(item.getKey(), vo);

			} else {
				DishesInfoListVo vo = itemDishes.get(0);
				vo.setRemark(item.getValue());
				vo.setDishesName("套餐" + item.getKey());
				vo.setDishesCode(item.getKey());
				vo.setComId(Long.valueOf(comId));
				vo.setDishesClassId(dishedClassId);
				vo.setStatus((byte) 1);
				vo.setSort(0);
				vo.setMerchId(mchId);
				vo.setRemark(item.getValue());
				Result r = payConfigClientService.updateDishes((JSONObject) JSONObject.toJSON(vo));
				log.info("更新菜品结果:" + r);
				result.put(item.getKey(), vo );
			}
		}


		return result;

	}


	/**
	 * 更新套餐,
	 * 返回key, DishesInfoListVo
	 */
	private Map<String, DishesInfoListVo> updateDishesC(Integer comId, Map<String, String> dishes, Integer dishedClassId, Integer mchId) {
		Map<String, DishesInfoListVo> result = new HashMap<>();
		List<DishesInfoListVo> allDishes = getAllDishes(Long.valueOf(comId));
		for (Map.Entry<String, String> item : dishes.entrySet()) {
			List<DishesInfoListVo> itemDishes = allDishes.stream().filter(dishesInfoListVo -> dishesInfoListVo.getDishesCode().equals(item.getKey())).collect(Collectors.toList());
			String[] parts = item.getValue().split("\\+");
			String dishesName = parts[0]+"("+item.getKey()+")";
			String[] newParts = Arrays.copyOfRange(parts, 1, parts.length);
			String remark = String.join("+",Arrays.asList(newParts));
			if (ObjectUtils.isEmpty(itemDishes)) {
				//插入菜品
				DishesInfoListVo vo = new DishesInfoListVo();
				vo.setRemark(remark);
				vo.setDishesName(dishesName);
				vo.setDishesCode(item.getKey());
				vo.setComId(Long.valueOf(comId));
				vo.setDishesClassId(dishedClassId);
				vo.setStatus((byte) 1);
				vo.setSort(0);
				vo.setMerchId(mchId);
				Result r = payConfigClientService.newDishes((JSONObject) JSONObject.toJSON(vo));
				log.info("插入菜品结果:【{}】参数,【{}】" + r,JSON.toJSONString(vo));
				LinkedHashMap data = (LinkedHashMap)r.getData();
				vo = JSON.parseObject(JSON.toJSONString(data), DishesInfoListVo.class);
				result.put(item.getKey(), vo);

			} else {
				DishesInfoListVo vo = itemDishes.get(0);
				vo.setRemark(remark);
				vo.setDishesName(dishesName);
				vo.setDishesCode(item.getKey());
				vo.setComId(Long.valueOf(comId));
				vo.setDishesClassId(dishedClassId);
				vo.setStatus((byte) 1);
				vo.setSort(0);
				vo.setMerchId(mchId);
				Result r = payConfigClientService.updateDishes((JSONObject) JSONObject.toJSON(vo));
				log.info("更新菜品结果:" + r);
				result.put(item.getKey(), vo );
			}
		}


		return result;

	}

	/**
	 * 更新价格
	 */
	private void updateDishesPrice(Integer comId, Map<String, String> dishes, Integer pointId, Map<String, DishesInfoListVo> allDishes, Integer mchId) {
		for (Map.Entry<String, String> item : dishes.entrySet()) {
			DishesInfoListVo itemDishes = allDishes.get(item.getKey());
      if (ObjectUtils.isEmpty(itemDishes)){
        continue;
      }
			//更新价格
			EntityDishesPrice price = new EntityDishesPrice();
			price.setComId(Long.valueOf(comId));
			price.setMerchId(mchId);
			price.setPrice(1500);
			price.setDishesId(itemDishes.getDishesId());
			price.setPointId(pointId);
			price.setPriceMode((byte) 20);
			Result r = payConfigClientService.setDishesPrice((JSONObject) JSONObject.toJSON(price));
			log.info("更新菜品价格:" + r);

		}

	}

	private Integer newMealPlan(Integer comId, Integer pointId, String planName,
															Date startDate, Date stopDate, Integer cycleDays,
															String remark, Integer mchId,String tagId) {

		EntityMealPlan plan = new EntityMealPlan();
		plan.setComId(Long.valueOf(comId));
		plan.setMerchId(mchId);
		plan.setPointId(pointId);
		plan.setPlanName(planName);
		plan.setStartDate(startDate);
		plan.setStopDate(stopDate);
		plan.setCycleDays(cycleDays);
		plan.setRemark(remark);
		plan.setReceiptType(0);
		plan.setReceiptIds("");
		plan.setPlanType("2");
		plan.setConsType(1);
		plan.setTagId(tagId);


		Result r = payConfigClientService.newMealPlan(plan);
		log.info("导入排餐计划：参数：【{}】，结果：【{}】",JSON.toJSONString(plan),r);
		if(!r.getCode().equals(200)){
			throw new RuntimeException(r.getMsg());
		}
		LinkedHashMap m = (LinkedHashMap) r.getData();
		return (Integer) m.get("mealplanId");

	}


	private List<DishesInfoListVo> getAllDishes(Long comId) {
		Result allDishes = payConfigClientService.getAllDishes(comId);
		List<LinkedHashMap> list = (List<LinkedHashMap>) allDishes.getData();
		List<DishesInfoListVo> reult = new ArrayList<>();
		list.forEach(item -> {
			DishesInfoListVo itemVo = JSON.parseObject(JSON.toJSONString(item), DishesInfoListVo.class);
			reult.add(itemVo);
		});

		return reult;

	}


	private Map<String, String> parseData(String s, XSSFSheet sheet, LocalDate startDay) {
		String num[] = s.split(":");
		Integer start = Integer.valueOf(num[0]);
		Integer end = Integer.valueOf(num[1]);


		Map<String, String> result = new HashMap<>();


		for (Integer ik = start; ik <= end; ik++) {
			XSSFRow row = sheet.getRow(ik);

			/**
			 * 一共读5列
			 */
			for (Integer k = 1; k < 6; k++) {
				String rowValue = row.getCell(k).getStringCellValue();
				LocalDate localDate = startDay.plusDays(k - 1);
				String key = localDate.format(DateTimeFormatter.ofPattern("dd"));
				String values = result.get(key);


				if (StringUtil.isEmpty(values)) {
					result.put(key, rowValue);
				} else {
					if (StringUtil.isNotEmpty(rowValue)) {
						values = values + "+" + rowValue;
						result.put(key, values);
					}
				}


			}

		}

		return result;
	}

	//解析Excel数据
	private MealPlanVo.MealExcelVo parseMealMenuCData(ExcelReader reader, LocalDate startDay,LocalDate endDay,String mealNo) {

		List<List<Object>> allData = reader.read();
		AtomicInteger currentWeek = new AtomicInteger(7);
		AtomicInteger currentTitleRow = new AtomicInteger(0);
		ZoneOffset offset = ZoneOffset.UTC;
		Date dateS = Date.from(startDay.atStartOfDay().toInstant(offset));
		Date dateE = Date.from(endDay.atStartOfDay().toInstant(offset));
		long days = DateUtil.between(dateS,dateE, DateUnit.DAY);
		Map<Integer,List<Date>> dayMap = new HashMap<>();
		Map<Integer,Map<Integer,List<MealPlanVo.WeekMealPlan>>> weekMealMap = new HashMap<>();
		Map<Integer,Map<Integer,String>> mealPlan = new HashMap<>();

		for (int i = 0; i <= days; i++) {
			Date day= DateUtil.offset(dateS, DateField.DAY_OF_MONTH,i);
			Instant instant = day.toInstant();
			LocalDate localDate = instant.atZone(ZoneId.systemDefault()).toLocalDate();
			int week = localDate.getDayOfWeek().getValue();
			if(ObjectUtil.isEmpty(dayMap.get(week))){
				dayMap.put(week,new ArrayList<>());
			}
			dayMap.get(week).add(day);
		}
		for (int k=0;k<allData.size();k++){
			List<Object> data = allData.get(k);
			for (int i=0;i<data.size();i++){
				//获取日期
				if(ObjectUtil.isEmpty(data.get(i))){
					continue;
				}
				String cellValue = data.get(i).toString();
				if(cellValue.startsWith("星期")){
					int week = getWeek(cellValue.replace("星期", "").trim());
					if(week == 0){
						continue;
					}
					currentWeek.set(week);
					currentTitleRow.set(k+1);
					continue;
				}
				if(i == 0){ continue; }
				if(StrUtil.isEmpty(cellValue)){ continue; }

				//获取套餐名称
				if(k == currentTitleRow.get()){
					if(ObjectUtil.isEmpty(mealPlan.get(currentWeek.get()))){
						mealPlan.put(currentWeek.get(),new HashMap<>());
					}

					if(ObjectUtil.isEmpty(mealPlan.get(currentWeek.get()).get(i))){
						mealPlan.get(currentWeek.get()).put(i,cellValue);
					}
//					continue;
				}

				if(ObjectUtil.isEmpty(weekMealMap.get(currentWeek.get()))){
					weekMealMap.put(currentWeek.get(),new HashMap<>());
				}

				if(ObjectUtil.isEmpty(weekMealMap.get(currentWeek.get()).get(i))){
					weekMealMap.get(currentWeek.get()).put(i,new ArrayList<>());
				}
				MealPlanVo.WeekMealPlan weekMealPlan = new MealPlanVo.WeekMealPlan();
				weekMealPlan.setMealName(cellValue);
				weekMealPlan.setMealKey(currentWeek.get()+"_"+k+"_"+i+"_"+mealNo);
				weekMealMap.get(currentWeek.get()).get(i).add(weekMealPlan);

			}
		}

		Map<String, MealPlanVo> dataMap = new HashMap<>();

		for (Integer key : dayMap.keySet()) {

			if(ObjectUtil.isNotEmpty(weekMealMap.get(key))){
				for (Integer weekMealMapKey : weekMealMap.get(key).keySet()){
					dayMap.get(key).forEach(date->{
						List<String> mealNames = weekMealMap.get(key).get(weekMealMapKey).stream().map(MealPlanVo.WeekMealPlan::getMealName).collect(Collectors.toList());
						List<MealPlanVo.WeekMealPlan> weekMealPlanList = weekMealMap.get(key).get(weekMealMapKey);
						MealPlanVo mealPlanVo = new MealPlanVo();
						mealPlanVo.setMealPlanName(mealPlan.get(key).get(weekMealMapKey));
						mealPlanVo.setMealNameList(mealNames);
						mealPlanVo.setMealNo(mealNo);
						mealPlanVo.setMealPlanKey(weekMealPlanList.get(0).getMealKey());
						mealPlanVo.setDate(date);
						dataMap.put(weekMealPlanList.get(0).getMealKey(),mealPlanVo);

					});

				}
			}
		}
		Map<String,String> abc = new HashMap<>();
		for (Integer weekMealMapKey : weekMealMap.keySet()){
			for (Integer weekMealMapKeyA :weekMealMap.get(weekMealMapKey).keySet() ) {
				List<MealPlanVo.WeekMealPlan> weekMealPlanList = weekMealMap.get(weekMealMapKey).get(weekMealMapKeyA);
				List<String> list = weekMealMap.get(weekMealMapKey).get(weekMealMapKeyA).stream().map(MealPlanVo.WeekMealPlan::getMealName).collect(Collectors.toList());
				abc.put(weekMealPlanList.get(0).getMealKey(),String.join("+",list));
			}
		}
		MealPlanVo.MealExcelVo mealExcelVo = new MealPlanVo.MealExcelVo();
		mealExcelVo.setMealData(abc);
		mealExcelVo.setDataMap(dataMap);
		return mealExcelVo;
	}

	private int getWeek(String weekDayString){
		int week = 0;
		if(weekDayString.contains("一")){
			week = 1;
		}else if(weekDayString.contains("二")){
			week = 2;
		}else if(weekDayString.contains("三")){
			week = 3;
		}
		else if(weekDayString.contains("四")){
			week = 4;
		}
		else if(weekDayString.contains("五")){
			week = 5;
		}
		else if(weekDayString.contains("六")){
			week = 6;
		}
		else if(weekDayString.contains("日")){
			week = 7;
		}
		return week;
	}
//	MealMenuC

	//数字转字母 1-26 ： A-Z
	private String numberToLetter(int num) {
		if (num <= 0) {
			return null;
		}
		String letter = "";
		num--;
		do {
			if (letter.length() > 0) {
				num--;
			}
			letter = ((char) (num % 26 + (int) 'A')) + letter;
			num = (int) ((num - num % 26) / 26);
		} while (num > 0);

		return letter;
	}


	private Map<String, String> convertMap(Map<String, Integer> seriaNumber, Integer size) {
		Map<String, String> reulst = new HashMap<>();
		int count = seriaNumber.keySet().size();
		for (int i = 1; i < count; i++) {
			String key = String.valueOf(i);
			String nextKey = String.valueOf(i + 1);
			Integer value = seriaNumber.get(key) + 1;
			Integer nextValue = seriaNumber.get(nextKey) - 2;
			reulst.put(key, value + ":" + nextValue);
		}
		Integer lastValue = seriaNumber.get(String.valueOf(count));

		reulst.put(String.valueOf(count), (lastValue + 1) + ":" + size);

		return reulst;
	}

	private List<EntityXfMealInfo> GetMealNameInfo(Integer comId) {
		Result result = payConfigClientService.GetMealNameInfo(Long.valueOf(comId));
		Map map = (Map) result.getData();
		List<LinkedHashMap> list = (List<LinkedHashMap>) map.get("list");
		List<EntityXfMealInfo> reult = new ArrayList<>();
		list.forEach(item -> {
			EntityXfMealInfo entityXfMealInfo = JSON.parseObject(JSON.toJSONString(item), EntityXfMealInfo.class);
			reult.add(entityXfMealInfo);
		});

		return reult;
	}

	/**
	 * 找到 序号开始的行
	 *
	 * @param sheet
	 * @return
	 */
	private Map<String, Integer> findserialNumber(XSSFSheet sheet) {
		Map<String, Integer> reulst = new HashMap<>();
		Integer count = 1;
		for (int i = 0; i <= sheet.getLastRowNum(); i++) {
			XSSFRow row = sheet.getRow(i);
      if (ObjectUtils.isEmpty(row)){
        continue;
      }
			XSSFCell rowStr = row.getCell(0);
			if (rowStr.getCellTypeEnum() == CellType.NUMERIC) {
				continue;
			}

			if (rowStr.getCellTypeEnum() == CellType.STRING) {
				if (!StringUtil.equals("序号", rowStr.getStringCellValue().trim())) {
					continue;
				}
			}

			reulst.put(String.valueOf(count), i);
			count = count + 1;
		}

		return reulst;

	}
}
